﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangLaptop.DAL;

namespace CuaHangLaptop.BIZ
{
    public class PhieuNhap_BIZ
    {
        int id;
        string maPhieuNhap;
        NhaSX_BIZ nhasx = new NhaSX_BIZ();
        DateTime ngaylap;
        decimal tongtien;
        bool tinhtrang;
        List<CTPhieuNhap_BIZ> list_ctpn = new List<CTPhieuNhap_BIZ>();

        public List<CTPhieuNhap_BIZ> List_CTPN
        {
            get 
            {
                return CTPhieuNhap_DAL.GetAll_CTPhieuNhap_ByPN(maPhieuNhap); 
            }
            set { list_ctpn = value; }
        }

        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        public string MaPhieuNhap
        {
            get { return maPhieuNhap; }
            set { maPhieuNhap = value; }
        }

        public NhaSX_BIZ Nhasx
        {
            get { return nhasx; }
            set { nhasx = value; }
        }

        public string TenNSX
        {
            get { return nhasx.TenNSX; }
        }

        public DateTime Ngaylap
        {
            get { return ngaylap; }
            set { ngaylap = value; }
        }

        public decimal Tongtien
        {
            get { return tongtien; }
            set { tongtien = value; }
        }

        public bool Tinhtrang
        {
            get { return tinhtrang; }
            set { tinhtrang = value; }
        }

        public PhieuNhap_BIZ()
        {
        }

        public PhieuNhap_BIZ(int _id, string maPN, string mansx, DateTime ngay, decimal total, bool tt)
        {
            id = _id;
            maPhieuNhap = maPN;
            nhasx = nhasx.Get_NSX_ByCode(mansx);
            ngaylap = ngay;
            tongtien = total;
            tinhtrang = tt;
        }

        public PhieuNhap_BIZ Get_PhieuNhap_ByCode(string _MaPhieuNhap)
        {
            return PhieuNhap_DAL.Get_PhieuNhap_ByCode(_MaPhieuNhap);
        }

        public List<PhieuNhap_BIZ> GetAll_PhieuNhap()
        {
            return PhieuNhap_DAL.GetAll_PN();
        }

        public void TotalPrice()
        {
            decimal _total = 0;
            foreach (var ctpn in List_CTPN)
                _total += ctpn.Soluong * ctpn.Laptop.Gia;
            tongtien = _total;
        }

        public void Insert_CTKho()
        {
            if (tinhtrang == true)
            {
                foreach (var ctpn in List_CTPN)
                {
                    ChiTietKho_BIZ ctkho = new ChiTietKho_BIZ();
                    int _id = ctkho.Get_Lasted_ID();
                    int tonkho = ctkho.Get_TonKho_Now(ctpn.Laptop.MaLaptop);
                    ctkho = new ChiTietKho_BIZ(-1, "CTK" + (_id + 1).ToString(), ctpn.Laptop.MaLaptop, ngaylap, tonkho + ctpn.Soluong);
                    ctkho.Save();
                    Kho_CTPN_DAL.Insert(ctpn.MaCTphieunhap, ctkho.MaCTKho);
                }
            }
        }

        public void Update_CTKho()
        {
            if (tinhtrang == true)
            {
                foreach (var ctpn in List_CTPN)
                {
                    bool check = false;
                    ChiTietKho_BIZ ctkho = new ChiTietKho_BIZ();
                    string _mactkho = Kho_CTPN_DAL.Get_CTKho_ByCTPN(ctpn.MaCTphieunhap);
                    if (_mactkho != "")
                    {
                        ctkho = ctkho.Get_CTKho_ByCode(_mactkho);
                        ctkho.Delete();
                        check = true;
                    }
                    int _id = ctkho.Get_Lasted_ID();
                    int tonkho = ctkho.Get_TonKho_Now(ctpn.Laptop.MaLaptop);
                    ctkho = new ChiTietKho_BIZ(-1, "CTK" + (_id + 1).ToString(), ctpn.Laptop.MaLaptop, ngaylap, tonkho + ctpn.Soluong);
                    ctkho.Save();
                    if (!check) Kho_CTPN_DAL.Insert(ctpn.MaCTphieunhap, ctkho.MaCTKho);
                }
            }
        }

        public void Save()
        {
            if (id == -1)
            {
                id = PhieuNhap_DAL.Insert(this);
                Insert_CTKho();
            }
            else
            {
                PhieuNhap_DAL.Update(this);
                Update_CTKho();
            }
            
        }

        public void Delete()
        {
            PhieuNhap_DAL.Delete(this);
        }

        public int Get_Lasted_ID()
        {
            return PhieuNhap_DAL.Get_Lasted_ID();
        }
    }
}
